<?php

namespace App\HttpController;

use App\Service\SmsService;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="短信验证码")
 */
class Sms extends BaseController
{
    /**
     * @ApiDescription(value="发送短信验证码")
     * @Api(name="发送短信验证码", path="/sms/send", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="mobile", description="收取短信手机号", required="手机号不能为空", notEmpty="手机号不能为空", from={JSON})
     * @Param(name="type", description="收取短信类型:注册.login,密码重置.password,手机号验证.check", required="", notEmpty="", from={JSON})
     * @ApiSuccess({"code": "200", "result": {"receive": "手机号", "type": "类型", "requestId": "任务id"}, "msg": "登陆成功"})
     * @return bool
     * @throws Throwable
     */
    public function send($mobile, $type): bool
    {
        $res = SmsService::getInstance()->send_sms($mobile,$type);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="验证短信验证码")
     * @Api(name="验证短信验证码", path="/sms/verify", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="requestId", required="", notEmpty="", description="验证码唯一标识", from={JSON})
     * @Param(name="code", type="int", required="", notEmpty="", description="验证码", from={JSON})
     * @ApiSuccess({"code": "200", "result": "success", "msg": "验证成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function check_sms($requestId,$code): bool
    {
        $res = SmsService::getInstance()->check_sms($requestId,$code);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}